<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>cate</title>
    <link rel="stylesheet" type="text/css" href="../jquery-easyui-1.5.3/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="../jquery-easyui-1.5.3/themes/icon.css">
    <link rel="stylesheet" type="text/css" href="../jquery-easyui-1.5.3/demo/demo.css">
    <script type="text/javascript" src="../jquery-easyui-1.5.3/jquery.min.js"></script>
    <script type="text/javascript" src="../jquery-easyui-1.5.3/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="../js/jquery.serializeObject.js"></script>
</head>
<body>
    <ul id="tt"></ul>

    <form id="ff" method="post">

        <div style="margin-bottom:20px;margin-top: 20px">
            <label>选择父文件:</label>
            <input  type='text' name="parentId" id="parentId" style="border-radius:4px;border:1px solid #ffa8a8;margin-left: 18px"/>
        </div>

        <div style="margin-bottom:20px">
            <input class="easyui-textbox" name="text" style="width:100%" data-options="label:'分类名称:',required:true,validType:'name'">
        </div>
        <div style="text-align:center;padding:5px 0">
            <a href="javascript:void(0)" class="easyui-linkbutton" onclick="addRootNode()" style="width:80px">添加根节点</a>
            <a href="javascript:void(0)" class="easyui-linkbutton" onclick="addChildNode()" style="width:80px">添加子节点</a>
            <a href="javascript:void(0)" class="easyui-linkbutton" onclick="removeNodes()" style="width:80px">删除节点</a>
		</div>
    </form>

    <script type="text/javascript">

        //tree
		$('#tt').tree({
            method:'get',
			url:'http://localhost:3000/cate/list',
            onClick(node){//点击获取节点id
//				console.log(node);//点击的文件夹节点的信息{id:'',text:''...}
			    $("#parentId").val(node._id);//设置#parentId的值
            }
		});

		//添加根节点函数
        function addRootNode() {
			$('#ff').form('submit',{
				onSubmit:function(){
					if($(this).form('enableValidation').form('validate')){
						var data = $('#ff').serializeObject();
//						console.log(data);
						delete data.parentId;
						$.ajax({//提交新增数据
							method:'post',
							url:'http://localhost:3000/cate/data',
							data:data,
						}).done(function (res) {
							$('#tt').tree('reload');
                        })
					};
				}
			});
		}

		//添加子根节点函数
		function addChildNode() {
			$('#ff').form('submit',{
				onSubmit:function(){
					if($(this).form('enableValidation').form('validate')){
						var data = $('#ff').serializeObject();

						$.ajax({//提交新增数据
							method:'post',
							url:'http://localhost:3000/cate/data',
							data:data,
						}).done(function (res) {
							$('#tt').tree('reload');
						})
					};
				}
			});
		}

		//删除子根节点函数
        function removeNodes() {
            var nodes = $("#tt").tree('getSelected');
//			console.log(nodes);//{_id: "59e764ec0bd5671ef00f2f8f", path: ",xxxx",children:"xxx"}
			$.ajax({//提交新增数据
				method:'get',
				url:'http://localhost:3000/return/'+nodes._id,
			}).done(function (res) {
				$('#tt').tree('reload');
			})
		}


    </script>
</body>
</html>
